package com.sq.partner.manager.controller;

import com.sq.partner.manager.common.Config;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.mybatis.model.ZJYDCommApplication;
import com.sq.partner.manager.mybatis.model.ZJYDCommPolicy;
import com.sq.partner.manager.service.CommissionService;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/commission")
public class CommissionController {

    private static Logger logger = LogUtil.getLog(CommissionController.class);

    @Autowired
    private CommissionService commissionService;

    @RequestMapping("/list")
    public String index(){
        return "/commission/index";
    }

    /**
     * 条件查询中建英大佣金申请单分页列表
     * @param pageIndex
     * @param size
     * @param application
     * @param approvalDateBegin
     * @param approvalDateEnd
     * @return
     */
    @RequestMapping("/queryApplicationPage")
    @ResponseBody
    public Page<ZJYDCommApplication> queryApplicationPage(Integer pageIndex, Integer size, ZJYDCommApplication application, String approvalDateBegin,String approvalDateEnd) throws Exception {

        Page page = beforeQueryApplicationPageByCriteria(pageIndex,size,application,approvalDateBegin,approvalDateEnd);

        Page<ZJYDCommApplication> applicationPage = commissionService.queryApplicationPage((Map) page.getType());
        return applicationPage;
    }

    /**
     * 跳转详情页面
     * @param map
     * @return
     */
    @RequestMapping("/detail")
    public String toDetail(HttpServletRequest request, ModelMap map) throws Exception {
        map.put("applyId", request.getParameter("applyId"));
        map.put("insurerId",request.getParameter("insurerId"));
        map.put("state", URLDecoder.decode(request.getParameter("state"), "utf-8"));
        return "commission/detail";
    }

    /**
     * 根据确认单号查询保单分页列表
     * @param applyId
     * @return
     */
    @RequestMapping("/queryPolicyPageByApplyId")
    @ResponseBody
    public Page queryPolicyPageByApplyId(Page page,String applyId) throws Exception {
        Map<String, Object> paramMap = SqlUtil.buildPageMap(page);
        paramMap.put("applyId", applyId);
        page.setMap(paramMap);
        page = commissionService.queryPolicyPageByApplyId(page);
        return page;
    }

    @RequestMapping("/downloadNotApply")
    @ResponseBody
    public Map downloadNotApply(String applyId) throws Exception{
        Map map = new HashMap();
        String url = commissionService.downloadNotApply(applyId);
        map.put("url",url);
        map.put("result","success");
        map.put("message","下载成功");
        return map;
    }

    @RequestMapping("/queryToCheck")
    @ResponseBody
    public ZJYDCommPolicy queryToCheck(String policyId) throws Exception {
        ZJYDCommPolicy zjydCommPolicy = commissionService.queryToCheck(policyId);
        return zjydCommPolicy;
    }

    @RequestMapping("/saveCheckPolicy")
    @ResponseBody
    public Result saveCheckPolicy(ZJYDCommPolicy zjydCommPolicy)throws Exception {
        commissionService.saveCheckPolicy(zjydCommPolicy);
        return Result.success("核准保存成功");
    }

    @RequestMapping("/downloadApply")
    @ResponseBody
    public Result downloadApply(String applyId)throws SQLException {
        String url = commissionService.downloadApply(applyId);
        return Result.success("下载成功",url);
    }


    @RequestMapping("/readyUploadUrl")
    @ResponseBody
    public Result readyUploadUrl(){
        String fileUploadUrl = Config.getProp("ZJYDSftp.FileUpload.url");
        return Result.success("读取成功",fileUploadUrl);
    }

    /**
     * 中建英大佣金申请单分页列表查询前数据校验
     * @param pageIndex
     * @param size
     * @param application
     * @param approvalDateBegin
     * @param approvalDateEnd
     * @return
     */
    private static Page beforeQueryApplicationPageByCriteria(Integer pageIndex, Integer size, ZJYDCommApplication application, String approvalDateBegin, String approvalDateEnd) {

        Page page = new Page();
        Map<String, Object> map = new HashMap<String, Object>();

        if (pageIndex >= 1 && size >= 1) {
            page.setCurrent(pageIndex);
            page.setSize(size);
            map = SqlUtil.buildPageMap(page);
        }

        if (!StringUtils.isEmpty(approvalDateBegin)) {
            approvalDateBegin = approvalDateBegin.concat(" 00:00:00");
        }
        if (!StringUtils.isEmpty(approvalDateEnd)) {
            approvalDateEnd = approvalDateEnd.concat(" 23:59:59");
        }
        map.put("insurerId", application.getInsurerId());
        map.put("id", application.getId());
        map.put("state", application.getState());
        map.put("approvalDateBegin", approvalDateBegin);
        map.put("approvalDateEnd", approvalDateEnd);

        page.setType(map);
        return page;
    }
}
